package server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tools.FileUtils;

public class PhotoFilePrepareServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out=resp.getWriter();
		out.println("<html><body><h1>This is a servlet test UploadContractServlet. welcome</h1></body></html>");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		PrintWriter out=resp.getWriter();
		String readpath = req.getSession().getServletContext().getRealPath("/");
		readpath += "/";
		
		String target_name = "assets/img/"+"tmp_"+req.getSession().getAttribute("username")+".jpg";
		String filepath = req.getParameter("filepath");
		out.println(filepath+"   "+target_name+" "+readpath);
		
		
		FileUtils fileUtils = new FileUtils();
		fileUtils.copyfile(filepath, readpath+target_name);
		
		req.getSession().setAttribute("filename", target_name);
		req.getRequestDispatcher("photoShow.jsp").forward(req, resp);
	}

}
